package beyjava.util;

import java.util.regex.Pattern;

/**
 * @author BEYJAVA
 * validation class
 */
public class Validator {
	static String regexDDMMYYYY="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$";
	/**
	 * @param aNumber
	 * @param aLow
	 * @param aHigh
	 * @throws IllegalArgumentException
	 * method to validate number is in wanted range
	 */
	public static void checkForRange( int aNumber, int aLow, int aHigh ) {
	    if ( !(aNumber >= aLow && aNumber <= aHigh) ) 
	      throw new IllegalArgumentException(aNumber + " not in range " + aLow + ".." + aHigh);
	}
	/**
	 * @param aText
	 * @throws IllegalArgumentException
	 * method to control whether the string has content or null. 
	 */
	public static void textHasContent( String aText ){
	    String EMPTY_STRING = "";
	    if(!((aText != null) && (!aText.trim().equals(EMPTY_STRING))))
	    	throw new IllegalArgumentException("Name has no content.");
	}
	/**
	 * @param check
	 * @return true if string is integer.
	 * method to control console entrance format 
	 */
	public static boolean isInt(String check){
		return  check.matches("^\\d+$");
	}
	/**
	 * @param aNumber
	 * @throws IllegalArgumentException
	 * method to check number is negative or not
	 */
	public static void checkForPositive(int aNumber) {
	    if (aNumber < 0) {
	      throw new IllegalArgumentException(aNumber + " is less than 0");
	    }
	}
	/**
	 * @param ans
	 * @throws IllegalArgumentException
	 * method to control answer of yes no question
	 */
	public static void checkForYesNoAnswer(String ans){
		if(!(ans.equals("Y")|| ans.equals("N")))
				throw new IllegalArgumentException("Your answer must be 'Y' or 'N'.");
	}
	/**
	 * @param date
	 * @throws IllegalArgumentException
	 * method to control date is valid or not
	 */
	public static void isValidDate(String date) {
        if(!Pattern.matches(regexDDMMYYYY, date))
	        	throw new IllegalArgumentException("Your date is invalid. Please check and enter date in dd-MM-yyyy format, again.");	    
    }
}

